package com.atguigu.gmall.activity.consumer;

import com.atguigu.gmall.activity.mapper.SeckillGoodsMapper;
import com.atguigu.gmall.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: 李旭
 * @Time: 15:41
 * @Description:
 *
 *  接收秒杀相关活动信息
 */
@Component
public class ActivityConsumer {


    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    //半夜12点接收消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_TASK_1,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_TASK),
            key = MqConst.ROUTING_TASK_1
    ))
    public void seckillImportRedis(Message message, Channel channel){
        try {
            //将秒杀表的数据导入缓存  当天的

            //1:审核通过
            //2:当天的商品
            //3:必须有剩余库存的商品
            //4:seckillGoodsMapper.selectList()
            //5:将结果集保存到Redis缓存中  （考虑 使用redis五大数据类型中哪个类型）
            //TODO 6:防止超卖
            //TODO 7:状态位
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
